package com.cwj.springboot_study.concurrentSecurity;

/**
 * @author wujie.chen
 * 2022/8/15 17:08
 */

public class DeadLockExample {
    private final Object nibbles = new Object();
    private final Object cash = new Object();
    public class Kidnapper extends Thread {
        public void run() {
            synchronized (nibbles) {
                synchronized (cash) {
                    take(cash);
                }
            }
        }
    }

    public class Negotiator extends Thread {
        public void run() {
            synchronized (cash) {
                synchronized (nibbles) {
                    take(nibbles);
                }
            }
        }
    }

    public void potentialDeadlock() {
        new Kidnapper().start();
        new Negotiator().start();
    }

    public void take(Object obj){
        System.out.println(Thread.currentThread().getName() + " " + obj);
    }
}
